package cn.community.care.module.system.service.task;

import cn.community.care.framework.common.exception.ServiceException;
import cn.community.care.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.community.care.framework.security.core.LoginUser;
import cn.community.care.module.system.controller.admin.orders.vo.OrdersSaveReqVO;
import cn.community.care.module.system.controller.admin.task.vo.TaskPageReqVO;
import cn.community.care.module.system.controller.admin.task.vo.TaskSaveReqVO;
import cn.community.care.module.system.dal.dataobject.project.ProjectDO;
import cn.community.care.module.system.dal.dataobject.task.TaskDO;
import cn.community.care.module.system.dal.dataobject.user.AdminUserDO;
import cn.community.care.module.system.dal.mysql.project.ProjectMapper;
import cn.community.care.module.system.dal.mysql.task.TaskMapper;
import cn.community.care.module.system.dal.mysql.user.AdminUserMapper;
import cn.community.care.module.system.service.orders.OrdersService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.pojo.PageParam;
import cn.community.care.framework.common.util.object.BeanUtils;


import static cn.community.care.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.community.care.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;

/**
 * 任务 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class TaskServiceImpl implements TaskService {

    @Resource
    private TaskMapper taskMapper;
    @Resource
    private AdminUserMapper userMapper;

    @Resource
    private ProjectMapper projectMapper;

    @Resource
    private OrdersService ordersService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long createTask(TaskSaveReqVO createReqVO) {
        LoginUser loginUser = getLoginUser();
        List<TaskDO> list = taskMapper.selectList(new LambdaQueryWrapperX<TaskDO>()
                .eq(TaskDO::getServiceId, createReqVO.getServiceId())
                .eq(TaskDO::getCreator, loginUser.getId())
                .ne(TaskDO::getStatus, "5"));
        if (!list.isEmpty()){
            throw new ServiceException(400, "当前人有未完成的同名的服务");
        }
        // 插入
        TaskDO task = BeanUtils.toBean(createReqVO, TaskDO.class);
        task.setServiceName(projectMapper.selectById(task.getServiceId()).getProjectName());
        task.setTaskName(projectMapper.selectById(task.getServiceId()).getProjectName());
        taskMapper.insert(task);
        OrdersSaveReqVO ordersSaveReqVO = new OrdersSaveReqVO();
        ordersSaveReqVO.setProductName(task.getTaskName());
        String price = projectMapper.selectById(task.getServiceId()).getPrice();
        //String转为BigDecimal
        ordersSaveReqVO.setPrice(new BigDecimal(price));
        ordersSaveReqVO.setProductId(task.getId());
        ordersSaveReqVO.setQuantity(1);
        ordersService.createOrders(ordersSaveReqVO);
        // 返回
        return task.getId();
    }

    @Override
    public void updateTask(TaskSaveReqVO updateReqVO) {
        // 校验存在
        validateTaskExists(updateReqVO.getId());
        // 更新
        TaskDO updateObj = BeanUtils.toBean(updateReqVO, TaskDO.class);
        taskMapper.updateById(updateObj);
    }

    @Override
    public void deleteTask(Long id) {
        // 校验存在
        validateTaskExists(id);
        // 删除
        taskMapper.deleteById(id);
    }

    private void validateTaskExists(Long id) {
        if (taskMapper.selectById(id) == null) {
//            throw exception(TASK_NOT_EXISTS);
        }
    }

    @Override
    public TaskDO getTask(Long id) {
        return taskMapper.selectById(id);
    }

    @Override
    public PageResult<TaskDO> getTaskPage(TaskPageReqVO pageReqVO) {
        return taskMapper.selectPage(pageReqVO);
    }

    @Override
    public void fenpei(TaskSaveReqVO updateReqVO) {
        Long assigneeId = updateReqVO.getAssigneeId();
        AdminUserDO user = userMapper.selectById(assigneeId);
        if (user.getStatus().equals("1")){
            throw new ServiceException(400, "该用户已被禁用");
        }
        updateReqVO.setAssigneeName(user.getNickname());
        updateReqVO.setStatus("2");
        TaskDO updateObj = BeanUtils.toBean(updateReqVO, TaskDO.class);
        taskMapper.updateById(updateObj);
    }

}